package cn.SiFuLan.controls;

import cn.SiFuLan.dao.IbrandDao;
import cn.SiFuLan.dao.IclassifDao;
import cn.SiFuLan.dao.IspuDao;
import cn.SiFuLan.dao.impl.brandDaoImpl;
import cn.SiFuLan.dao.impl.classifDaoImpl;
import cn.SiFuLan.dao.impl.spuDaoImpl;
import cn.SiFuLan.entity.brandEntity;
import cn.SiFuLan.entity.classifyEntity;
import cn.SiFuLan.entity.spuEntity;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/admin/add.shtml")
public class addServlt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IbrandDao ibrandDao=new brandDaoImpl();
        brandEntity brand=new brandEntity();
        List<brandEntity> list= ibrandDao.All();

        IclassifDao iclassifDao=new classifDaoImpl();
        List<classifyEntity> list1=iclassifDao.All();

        request.setAttribute("list1",list1);
        request.setAttribute("list",list);
        request.getRequestDispatcher("insert.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.实例化对象
        SmartUpload smart = new SmartUpload();
        smart.setCharset("utf-8");
//2.初始化上传
        smart.initialize(this.getServletConfig(),request,response);
        try {
            //3.准备上传
            smart.upload();
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

        String path = request.getSession().getServletContext().getRealPath("/upload");
        File file = smart.getFiles().getFile(0);
        System.out.println(path);

        System.out.println("获取文件名称："+file.getFileName());
        System.out.println("获取文件大小："+file.getSize());
        System.out.println("获取文件类型："+file.getFileExt());
        System.out.println(path);

//获取扩展名
        String ext = file.getFileExt();//png   jpg
//4.保存文件1\\\\\\\\
//mall/upload/2000202020.png
        String fileName = new Date().getTime()+"."+ext;
        try {
            file.saveAs(path+"/"+fileName);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

        System.out.println(fileName);


        IspuDao ispuDao=new spuDaoImpl();
        spuEntity spuEntity=new spuEntity();
        spuEntity.setSpuimg(fileName);
        spuEntity.setSpu_Name(smart.getRequest().getParameter("name"));
        spuEntity.setTitle(smart.getRequest().getParameter("title"));
        String cid=smart.getRequest().getParameter("cname");
        if(cid!=null && !cid.equals("")){
            spuEntity.setClass_id(Integer.parseInt(cid));
        }
        String bid=smart.getRequest().getParameter("binfo");
        if(bid!=null && !bid.equals("")){
            spuEntity.setBrand_id(Integer.parseInt(bid));
        }
        spuEntity.setSpu_Info(smart.getRequest().getParameter("sinfo"));
        spuEntity.setPacking(smart.getRequest().getParameter("packing"));
        spuEntity.setService(smart.getRequest().getParameter("service"));
        String tid=smart.getRequest().getParameter("takedown");
        if(tid!=null && !tid.equals("")){
            spuEntity.setTakedown(Integer.parseInt(tid));
        }
        spuEntity.setAddTime(new Date());
        spuEntity.setUpdateTime(null);

        if(ispuDao.add(spuEntity)){
            response.sendRedirect("index.do");
        }else {
            System.out.println("失败");
        }




    }
}
